package com.leqicheng.tenon;

import org.apache.http.Header;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.leqicheng.tenon.util.ContextProvider;
import com.leqicheng.tenon.util.GeneralCallBack;
import com.leqicheng.tenon.util.NetworkUtil;
import com.leqicheng.tenon.util.RequestStatus;
import com.leqicheng.tenon.util.RequestTypeEnum;

public class TenonGameListView extends FrameLayout implements GeneralCallBack {
    private WebView mWebView;
    private Context mContext;
    private String mChannelKey;
    private String mUserId;

    public TenonGameListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        ContextProvider.initIfNotInited(mContext.getApplicationContext());
        NetworkUtil.init();
        this.setBackgroundColor(Color.WHITE);
        mWebView = new WebView(mContext);
        // 设置编码
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        // 支持js
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 设置背景颜色 透明
        mWebView.setBackgroundColor(Color.WHITE);
        // 载入js
        this.addView(mWebView);
    }

    public void init(Context context, String channelKey) {
        mChannelKey = channelKey;
        getListPage();
    }

    public void init(Context context, String channelKey, String userId) {
        mChannelKey = channelKey;
        mUserId = userId;
        getListPage();
    }

    private void getListPage() {
        ListPageHttpRequest listPageHttpRequest = new ListPageHttpRequest(
                mContext, this);
        listPageHttpRequest.postRequest(mChannelKey);
    }


    @Override
    public void onComplete(RequestTypeEnum requestType, int status,
            String responseBody) {
        switch (status) {
        case RequestStatus.NETWORK_ERROR:
            Toast.makeText(mContext, "网络错误", Toast.LENGTH_SHORT).show();
            break;
        case RequestStatus.OBTAIN_SUCCESS:
            String url = JSON.parseObject(responseBody).getString("url");
            loadWebView(url);
        default:
            break;
        }
    }

    private void loadWebView(String url) {
        // 设置本地调用对象及其接口
        mWebView.addJavascriptInterface(new TenonJavaScript(mContext,
                mChannelKey, mUserId), "tenonsdk");
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                showErrorView();
            }

        });
    }

    protected void showErrorView() {
        this.removeView(mWebView);
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("无法获取游戏列表，请检查网络");
        textView.setTextSize(20);
        this.addView(textView);
    }

}
